package com.wangxq.yd_base.mvp.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.database.MMkvHelper;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.orhanobut.logger.Logger;
import com.wangxq.mylibrary.dialog.DialogTools;
import com.wangxq.mylibrary.dialog.NoticeDialog;
import com.wangxq.yd_base.R;
import com.wangxq.yd_base.di.component.DaggerTestComponent;
import com.wangxq.yd_base.di.module.TestModule;
import com.wangxq.yd_base.mvp.contract.TestContract;
import com.wangxq.yd_base.mvp.presenter.TestPresenter;
import com.zlx.module_base.appupdate.UpdateManager;
import com.zlx.widget.LimitLengthTextView.LimitLengthTextView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class TestActivity extends BaseActivity<TestPresenter> implements TestContract.View {
    @Inject
    TestPresenter mPresenter;
    @BindView(R.id.tool_bar)
    Toolbar tool_bar;
    private static UpdateManager manager;
    @BindView(R.id.tvModel)
    TextView tvModel;
    @BindView(R.id.llTicketContentLimitLenth)
    LimitLengthTextView llTicketContentLimitLenth;

    //    private String url="http://139.9.246.226:8081/DTCG/pdaAppUpdate/pda_sb_version.xml";
    private String url = "http://139.9.246.226:8081/DTCG/pdaAppUpdate/SH17_release_V1.6_C7.apk";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerTestComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .testModule(new TestModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_test; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        llTicketContentLimitLenth.setLimit_number(100);
        llTicketContentLimitLenth.setHintText("故障描述");
        llTicketContentLimitLenth.setMaxline_number(2);

        tvModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean offLineMode = MMkvHelper.getInstance().getCurrentMode();
                if (offLineMode) {
                    Logger.e("当前在线模式");
                } else {
                    Logger.e("当前离线模式");
                }
            }
        });

        findViewById(R.id.tvLogin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean offLineMode = MMkvHelper.getInstance().getOffLineMode();
                if (offLineMode) {
                    Logger.e("启用离线模式");

                } else {
                    Logger.e("启用在线模式");
                    MMkvHelper.getInstance().currentMode(false);
                }

//             Thread thread=new Thread(new Runnable() {
//                 @Override
//                 public void run() {
//                     try {
//                         new Progress().run(url, new PercentListener() {
//                             @Override
//                             public void currentPercent(int percent,Boolean finish,String url) {
//                                 Log.e("当前进度",percent+"%"+finish);
//                                 if(percent==100&&finish){
//                                     //升级文件
//                                     if(url.contains(".xml")){
//
//                                     }
//                                 }
//                             }
//                         });
//                     } catch (Exception e) {
//                         e.printStackTrace();
//                     }
//                 }
//             });
//             thread.start();

//             mPresenter.Login("sysadmin","123",false);
            }
        });

        setToolBar(tool_bar, "我是ToolBar");

        DialogTools.INSTANCE.showCommomDialog(getSupportFragmentManager(), new NoticeDialog.Builder()
                .setDialogButtonListener(new NoticeDialog.DialogButtonClickListener() {
                    @Override
                    public void rightOnClick(NoticeDialog noticeDialog) {

                    }

                    @Override
                    public void leftOnClick(NoticeDialog noticeDialog) {

                    }
                }).setContentText("确认删除该文件吗？")
        );


    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @OnClick(R.id.tvLogin)
    public void tvLogin() {
        mPresenter.Login("sysadmin", "123", false);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void toMainActivity() {
        mPresenter.getMenu("http://139.9.246.226:8088/DTCG/zy/emZyApd/getApdMenu.action");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (manager == null) {
            manager = new UpdateManager(this);
        }
        manager.cancelUpdate = false;
        manager.compulsiveUpdate = false;
        Dialog noticeDialog = manager.getNoticeDialog();
        if (noticeDialog == null || (noticeDialog != null && !noticeDialog.isShowing())) {
            // 检查软件更新
            manager.checkUpdate();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
